package cn.iocoder.yudao.module.iot.service.warnpolicy;

import cn.iocoder.yudao.module.iot.enums.IotErrorCodeConstants;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.iot.controller.admin.warnpolicy.vo.*;
import cn.iocoder.yudao.module.iot.dal.dataobject.warnpolicy.WarnPolicyDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.iot.dal.mysql.warnpolicy.WarnPolicyMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 设备消防策略 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class WarnPolicyServiceImpl implements WarnPolicyService {

    @Resource
    private WarnPolicyMapper warnPolicyMapper;

    @Override
    public Long createWarnPolicy(WarnPolicySaveReqVO createReqVO) {
        // 插入
        WarnPolicyDO warnPolicy = BeanUtils.toBean(createReqVO, WarnPolicyDO.class);
        warnPolicyMapper.insert(warnPolicy);
        // 返回
        return warnPolicy.getId();
    }

    @Override
    public void updateWarnPolicy(WarnPolicySaveReqVO updateReqVO) {
        // 校验存在
        validateWarnPolicyExists(updateReqVO.getId());
        // 更新
        WarnPolicyDO updateObj = BeanUtils.toBean(updateReqVO, WarnPolicyDO.class);
        warnPolicyMapper.updateById(updateObj);
    }

    @Override
    public void deleteWarnPolicy(Long id) {
        // 校验存在
        validateWarnPolicyExists(id);
        // 删除
        warnPolicyMapper.deleteById(id);
    }

    @Override
    public void deleteWarnPolicyByPhone(String phone) {
        warnPolicyMapper.deleteByPhone(phone);
    }

    private void validateWarnPolicyExists(Long id) {
        if (warnPolicyMapper.selectById(id) == null) {
            throw exception(IotErrorCodeConstants.WARN_POLICY_NOT_EXISTS);
        }
    }

    @Override
    public WarnPolicyDO getWarnPolicy(Long id) {
        return warnPolicyMapper.selectById(id);
    }

    @Override
    public PageResult<WarnPolicyDO> getWarnPolicyPage(WarnPolicyPageReqVO pageReqVO) {
        return warnPolicyMapper.selectPage(pageReqVO);
    }

    @Override
    public List<WarnPolicyDO> getWarnPolicyList(String deviceName) {
        return warnPolicyMapper.getWarnPolicyList(deviceName);
    }

    @Override
    public WarnPolicyDO getByPhone(String phone,String deviceName) {
        return warnPolicyMapper.getByPhone(phone,deviceName);
    }

}